package tools.settings.gui;

import java.awt.Component;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileNameExtensionFilter;
import tools.settings.SettingsAdapter;
import tools.settings.SettingsReaderException;
import tools.settings.VerifySettings;

/**
 *
 * @author Lynne Vandousselaere
 */
public class JSettingsPane {

    private static SettingsAdapter sa = SettingsAdapter.getInstance();
    public static boolean information_msg_on = true;
    public static boolean verifyFilesOnAdd = true;

    public static void setDefaultFolderDialog(Component parent) {
        JFileChooser chooser = new JFileChooser(sa.getDefault_folder());
        FileNameExtensionFilter filter = new FileNameExtensionFilter(
                "XML-files", "xml");
        chooser.setFileFilter(filter);
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        int returnVal = chooser.showDialog(parent, "Set as default directory");

        if (returnVal == JFileChooser.APPROVE_OPTION) {
            sa.setDefaultFolder(chooser.getSelectedFile());
            if (information_msg_on)
                JOptionPane.showMessageDialog(chooser,
                        "Default directory changed to \"" + chooser.getSelectedFile().getPath() + "\"",
                        "Default directory changed.",
                        JOptionPane.INFORMATION_MESSAGE);
        }
    }

    public static void checkXMLFile(Component parent) {
        JFileChooser chooser = new JFileChooser(sa.getDefault_folder());
        FileNameExtensionFilter filter = new FileNameExtensionFilter(
                "XML-files", "xml");
        chooser.setFileFilter(filter);
        int returnVal = chooser.showOpenDialog(parent);

        if (returnVal == JFileChooser.APPROVE_OPTION)
            VerifySettings.getInstance().doXMLcheck(chooser.getSelectedFile()).showMessageDialog(parent, information_msg_on);

    }

    public static void addXMLFiles(Component parent) {
        JFileChooser chooser = new JFileChooser(sa.getDefault_folder());
        FileNameExtensionFilter filter = new FileNameExtensionFilter(
                "XML-files", "xml");
        chooser.setMultiSelectionEnabled(true);
        chooser.setFileFilter(filter);
        int returnVal = chooser.showOpenDialog(parent);

        File[] farray = {chooser.getSelectedFile()};
        if (chooser.getSelectedFiles().length != 0)
            farray = chooser.getSelectedFiles();
        boolean[] error = new boolean[farray.length];

        if (returnVal == JFileChooser.APPROVE_OPTION) {
            if (verifyFilesOnAdd)
                for (int i = 0; i < farray.length; i++) {
                    VerifySettings.SettingsMessage msg = VerifySettings.getInstance().doXMLcheck(farray[i]);
                    msg.showMessageDialog(parent, information_msg_on);
                    error[i] = false;
                    if (!msg.getVerified()) {
                        System.out.println("not verified " + farray[i]);
                        error[i] = true;
                    }
                }
            try {
                for (int i = 0; i < farray.length; i++)
                    if (!error[i])
                        sa.addSettings(farray[i]);
            } catch (SettingsReaderException ex) {
                JOptionPane.showMessageDialog(parent, "Exception occurred, not sure why. Maybe the file is unreadable. \nPlease debug. \n\n" + ex.toString(),
                        "Error while adding a settings XML file", JOptionPane.ERROR_MESSAGE);
                return;
            }

            // Nothing happened ! Everything OK...
            String bestand = "";
            String errors = "";
            for (int i = 0; i < farray.length; i++)
                if (error[i])
                    errors += farray[i].getPath() + "\n";
                else
                    bestand += farray[i].getPath() + "\n";
            if (information_msg_on)
                JOptionPane.showMessageDialog(parent, "Following files where added: " + bestand,
                        "Success!!", JOptionPane.INFORMATION_MESSAGE);
            JOptionPane.showMessageDialog(parent, "Following files where not verified and thus not added: " + errors,
                    "Success!!", JOptionPane.ERROR_MESSAGE);


        }
    }
}
